package com.egao.common.manage.college;

import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.cooperative.constants.CooperativeProjectConstants;
import com.egao.common.module.cooperative.entity.CooperativeProject;
import com.egao.common.module.cooperative.entity.CooperativeProjectActivity;
import com.egao.common.module.cooperative.entity.CooperativeProjectActivityStudent;
import com.egao.common.module.cooperative.entity.CooperativeProjectRecord;
import com.egao.common.module.cooperative.entity.vo.CooperativeProjectActivityListVO;
import com.egao.common.module.cooperative.entity.vo.CooperativeProjectActivityStudentListVO;
import com.egao.common.module.cooperative.entity.vo.CooperativeProjectListVO;
import com.egao.common.module.cooperative.entity.vo.CooperativeProjectRecordListVO;
import com.egao.common.module.cooperative.service.CooperativeProjectActivityService;
import com.egao.common.module.cooperative.service.CooperativeProjectActivityStudentService;
import com.egao.common.module.cooperative.service.CooperativeProjectRecordService;
import com.egao.common.module.cooperative.service.CooperativeProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 产教融合 导师接口
 * @author root
 */
@Api(tags = "产教融合 导师接口")
@RestController
@RequestMapping("${api}")
public class CollegeCooperativeController extends BaseController {

    @Autowired
    private CooperativeProjectService cooperativeProjectService;
    @Autowired
    private CooperativeProjectActivityService cooperativeProjectActivityService;
    @Autowired
    private CooperativeProjectActivityStudentService cooperativeProjectActivityStudentService;
    @Autowired
    private CooperativeProjectRecordService cooperativeProjectRecordService;

    private final static String title = "导师产教融合";

    @PreAuthorize("hasAuthority('college:cooperative:list')")
    @ApiOperation(value = "产教融合导师项目列表")
    @ApiPageParam
    @GetMapping("/college/cooperative/page")
    public PageResult<CooperativeProjectListVO> page(HttpServletRequest request) {
        PageParam<CooperativeProject> pageParam = new PageParam<>(request);
        pageParam.put("teacherId", getLoginUserId());
        return new PageResult<>(cooperativeProjectService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('college:cooperative:detail')")
    @ApiOperation(value = "产教融合项目详情")
    @GetMapping("/college/cooperative/detail")
    public JsonResult detail(Integer id) {
        return JsonResult.ok().setData(cooperativeProjectService.detail(id, CooperativeProjectConstants.RR_TEACHER));
    }

    @PreAuthorize("hasAuthority('college:cooperative:detail')")
    @ApiOperation(value = "产教融合 项目活动列表")
    @ApiPageParam
    @GetMapping("/college/cooperative/activity-page")
    public PageResult<CooperativeProjectActivityListVO> activityPage(HttpServletRequest request) {
        PageParam<CooperativeProjectActivity> pageParam = new PageParam<>(request);
        return new PageResult<>(cooperativeProjectActivityService.listPageActivity(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('college:cooperative:detail')")
    @ApiOperation(value = "产教融合 项目风采列表")
    @ApiPageParam
    @GetMapping("/college/cooperative/shine-page")
    public PageResult<CooperativeProjectActivityListVO> shinePage(HttpServletRequest request) {
        PageParam<CooperativeProjectActivity> pageParam = new PageParam<>(request);
        return new PageResult<>(cooperativeProjectActivityService.listPageShine(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('college:cooperative:detail')")
    @ApiOperation(value = "产教融合 项目工作动态列表")
    @ApiPageParam
    @GetMapping("/college/cooperative/dynamic-page")
    public PageResult<CooperativeProjectActivityListVO> dynamicPage(HttpServletRequest request) {
        PageParam<CooperativeProjectActivity> pageParam = new PageParam<>(request);
        return new PageResult<>(cooperativeProjectActivityService.listPageDynamic(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('college:cooperative:detail')")
    @ApiOperation(value = "产教融合 项目项目记录列表")
    @ApiPageParam
    @GetMapping("/college/cooperative/record-page")
    public PageResult<CooperativeProjectRecordListVO> recordPage(HttpServletRequest request) {
        PageParam<CooperativeProjectRecord> pageParam = new PageParam<>(request);
        return new PageResult<>(cooperativeProjectRecordService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('college:cooperative:detail')")
    @ApiOperation(value = "产教融合 项目活动详情")
    @ApiPageParam
    @GetMapping("/college/cooperative/activity-detail")
    public JsonResult activityDetail(Integer id) {
        return JsonResult.ok().setData(cooperativeProjectActivityService.detail(id, CooperativeProjectConstants.RR_TEACHER));
    }

    @PreAuthorize("hasAuthority('college:cooperative:delete')")
    @OperLog(value = title, desc = "删除活动", result = true)
    @ApiOperation(value = "删除活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "CooperativeProjectActivity", value = "CooperativeProjectActivity", required = true, dataType = "CooperativeProjectActivity")
    })
    @PutMapping("/college/cooperative/delete-activity")
    public JsonResult delete(@RequestBody CooperativeProjectActivity cooperativeProjectActivity) {
        return cooperativeProjectActivityService.deleteCooperativeProjectActivity(cooperativeProjectActivity.getId(), CooperativeProjectConstants.RR_TEACHER);
    }

    @PreAuthorize("hasAuthority('college:cooperative:detail')")
    @OperLog(value = title, desc = "保存活动", result = true)
    @ApiOperation(value = "保存活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "CooperativeProjectActivity", value = "CooperativeProjectActivity", required = true, dataType = "CooperativeProjectActivity")
    })
    @PostMapping("/college/cooperative/save")
    public JsonResult save(@RequestBody CooperativeProjectActivity cooperativeProjectActivity) {
        if (cooperativeProjectActivity.getId() == null) {
            return cooperativeProjectActivityService.addCooperativeProjectActivity(cooperativeProjectActivity, CooperativeProjectConstants.RR_TEACHER);
        }
        return cooperativeProjectActivityService.updateCooperativeProjectActivity(cooperativeProjectActivity, CooperativeProjectConstants.RR_TEACHER);
    }

    @PreAuthorize("hasAuthority('college:cooperative:detail')")
    @ApiOperation(value = "产教融合 学生报名列表")
    @ApiPageParam
    @GetMapping("/college/cooperative/student-page")
    public PageResult<CooperativeProjectActivityStudentListVO> studentPage(HttpServletRequest request) {
        PageParam<CooperativeProjectActivityStudent> pageParam = new PageParam<>(request);
        return new PageResult<>(cooperativeProjectActivityStudentService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('college:cooperative:detail')")
    @OperLog(value = title, desc = "签到导入", result = true)
    @ApiOperation("签到导入")
    @Transactional
    @PostMapping("/college/cooperative/import")
    public JsonResult importStudent(MultipartFile file, Integer id) {
        return cooperativeProjectActivityStudentService.importStudent(file, id);
    }
}
